package Classes.GameUtils
{
	import Classes.GameDatas.GD_HavenWall;
	import Classes.GameDatas.GD_MapGrid;
	import Classes.GameDatas.GD_Mission;
	import Classes.GameDatas.GD_Player;
	import Classes.GameViews.GV_Map;
	import Classes.GameViews.ViewIcons.VI_VectorIcon;
	import Classes.GameViews.ViewPanels.VP_Main;
	import Classes.GameViews.ViewPanels.VP_Mission;
	import Classes.GameViews.ViewPanels.VP_NPCDialog;
	import Classes.GameViews.ViewPanels.VP_NPCFlashPlayView;
	import Classes.GameViews.ViewPanels.VP_PorgramNpcDialogue;
	import Classes.staticinfo.PlatformInfo;
	
	import com.greensock.TweenLite;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.StageDisplayState;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.ui.Mouse;
	import flash.utils.Timer;
	
	/** 
	 * 特殊任务展现处理类
	 * @author thy
	 * @version 1.0.0 
	 * 创建时间：Mar 27, 2012 9:58:19 AM 
	 */ 
	public class Deal_SpecialMission
	{
		private var otherArr:Array=new Array();
		private var gd_miss:GD_Mission;
		private var isOpenNpc:Boolean=true;
		
		private static var _instance:Deal_SpecialMission;
		public function Deal_SpecialMission(){}
		
		/**
		 * 处理领取任务 
		 * @param msgObj
		 */		
		public function hanldMIssion(msgObj:Object):void
		{
			this.index=1;
			this.isHanldOverMissionFlag=false;
			this.isOpenNpc=true;
			otherArr=[];
			var missionID:Number=Number(msgObj.missionID);//msgObj.missionID  11000017
			gd_miss=GD_Player.instance.missions[missionID] as GD_Mission;
			//修改任务状态
			gd_miss.missionStatue=1;
			//如果类型为 11 或者 12  13  15就做特殊处理   ---牵制条件
			this.otherArr=gd_miss.otherCountsAry;
			if(null!=otherArr && otherArr.length>0)
			{
				openMissionDialog(otherArr[0]);
			}else{
				if(this.gd_miss.taskStyle!=10)
				VP_Mission.instance.setMission(this.gd_miss);
			}
		}
		
		private var isHanldOverMissionFlag:Boolean=false;	
		private var overMissionFun:Function;
		private var overMisObj:Object;
		/**
		 * 处理完成任务  
		 * @param msgObj
		 * @param overMissionFun
		 */	
		public function hanldOverMisstion(msgObj:Object,_overMissionFun:Function):void
		{
			this.index=1;
			this.isHanldOverMissionFlag=true;
			this.overMisObj=msgObj;
			this.isOpenNpc=true;
			this.overMissionFun=_overMissionFun;
			otherArr=[];
			var missionID:Number=Number(msgObj.missionID);//msgObj.missionID
			gd_miss=GD_Player.instance.missions[missionID] as GD_Mission;
			this.otherArr=gd_miss.rewardSpectialCountsAry;
			if(null!=otherArr && otherArr.length>0)
			{
				openMissionDialog(otherArr[0]);
			}else{
				this.overMissionFun(this.overMisObj);
				this.overMisObj=null;
				this.isHanldOverMissionFlag=false;
				this.overMissionFun=null;
			}
			
		}
		
		private function openMissionDialog(obj:Object):void
		{
			if(Number(obj.countSecondType)==11)//对话
			{
				var disObj:DisplayObject=this.creatNpcIcon(obj.par1,obj.par2);//par1资源    par2资源里面的元件名称
				disObj.scaleX=0.5;
				disObj.scaleY=0.5;
				VP_NPCDialog.instance.setDialog(disObj,obj.par3,callBackFun);//par3 描述
				if(this.isOpenNpc)
				{
					this.isOpenNpc=false;
					VP_NPCDialog.instance.openNpc();
				}
				
			}else if(Number(obj.countSecondType)==12)//动画
			{

				VP_NPCDialog.instance.closeWin();
				this.isOpenNpc=true;
				var mcobj:DisplayObject=this.creatNpcIcon(obj.par1,obj.par2);
				VP_NPCFlashPlayView.instance.setFlashPlayInfo(mcobj,callBackFun);
			}else if(Number(obj.countSecondType)==13 && GD_Player.instance.character.characterState!=2){//锁屏

				VP_NPCDialog.instance.closeWin();
				this.isOpenNpc=true;
				PlatformInfo.instance.isMoveScreen=true;
				
				if(Number(obj["par"+index])==-1 || isNaN(obj["par"+index]))return;
					
				Mouse.hide();
				//移动屏幕
				this.yipingFun(Number(obj["par"+index]));
				
			}else if(Number(obj.countSecondType)==15){//15进度弹框
				VP_NPCDialog.instance.closeWin();
				this.isOpenNpc=true;
				//标题
				var title:DisplayObject=this.creatNpcIcon(obj.par1,obj.par2);
				var context:String=String(obj.par3);
				while(context.indexOf("||")!=-1)
				{
					context=context.replace("||","\n");
				}
				var contextNpc:DisplayObject=this.creatNpcIcon(obj.par1,obj.par4);
				var programValue:int=int(obj.par5);
				VP_PorgramNpcDialogue.isOpenProgramDialogue=true;
				VP_PorgramNpcDialogue.instance.setPorgramNpcDialogueInfo(title,context,contextNpc,programValue,callBackFun);
				VP_PorgramNpcDialogue.instance.openNpc();
				if(programValue>0)
				{
					var tmpT:Timer=new Timer(1000,1);
					tmpT.addEventListener(TimerEvent.TIMER_COMPLETE,complete);
					tmpT.start();
					function complete(e:Event):void
					{
						tmpT.stop();
						tmpT.removeEventListener(TimerEvent.TIMER_COMPLETE,complete);
						tmpT=null;
						VP_PorgramNpcDialogue.instance.controlProgramBar(programValue);//programValue
					}
				}
			}else{
				VP_NPCDialog.instance.closeWin();
				this.isOpenNpc=true;
				callBackFun();
			}
		
		}
		private function callBackFun():void
		{
			if(VP_PorgramNpcDialogue.isOpenProgramDialogue)
			{
				VP_PorgramNpcDialogue.instance.closeWin();
				VP_PorgramNpcDialogue.isOpenProgramDialogue=false;
			}
			this.otherArr.splice(0,1);
			if(null!=otherArr && otherArr.length>0)
			{
				openMissionDialog(otherArr[0]);
			}else{
				this.isOpenNpc=true;
				VP_NPCDialog.instance.closeWin();
				if(this.isHanldOverMissionFlag)
				{
					if(null!=this.overMissionFun)
					{
						this.overMissionFun(this.overMisObj);
						this.overMisObj=null;
						this.isHanldOverMissionFlag=false;
						this.overMissionFun=null;
					}
				}else{
					if(this.gd_miss.taskStyle!=10)
					{
						VP_Mission.instance.setMission(this.gd_miss);
					}
					//new guide
					if(this.gd_miss.missionType==11000001)
					{
						GD_HavenWall.inbreakCheck();
					}
				}
				
			}
		}
		
		private var index:Number=1;
		private var maskSp:Sprite;
		private function yipingFun(value:Number):void
		{
			if(value==-1)return;
			index++;
			var atx:int=value % 200;
			var aty:int=value / 200;
			var gd_Grid:GD_MapGrid=GV_Map.instance.getCoordinateGrid(new Point(atx, aty))
			var isoPoint:Point=new Point(gd_Grid.atx*GV_Map.instance.gridSize,gd_Grid.aty*GV_Map.instance.gridSize);
			var screenPoint:Point=GU_IsoUtils.isoToScreen(isoPoint);
			var toy:Number=-(screenPoint.y+GV_Map.instance.layerMapTargets.y)+VP_Main.instance.stage.stageWidth/2;
			var tox:Number=-(screenPoint.x+GV_Map.instance.layerMapTargets.x)+VP_Main.instance.stage.stageHeight/2;
			
			if(VP_Main.instance.parent.stage.displayState==StageDisplayState.FULL_SCREEN)
			{
				toy=-(screenPoint.y+GV_Map.instance.layerMapTargets.y)+VP_Main.instance.stage.stageWidth*0.4;
				tox=-(screenPoint.x+GV_Map.instance.layerMapTargets.x)+VP_Main.instance.stage.stageHeight*0.65;
			}
			
			var maxTox:Number	=	GV_Map.instance.width/2;
			var maxToy:Number	=	GV_Map.instance.height-GV_Map.instance.stage.stageHeight;
			toy	= toy>0?0:toy;
			toy = toy<-maxToy?-maxToy:toy;
			
			tox = tox>maxTox?maxTox:tox;
			tox = tox<-(maxTox-GV_Map.instance.stage.stageWidth)?-(maxTox-GV_Map.instance.stage.stageWidth):tox;
			
			TweenLite.to(GV_Map.instance,4,{y:toy,x:tox,delay:0.5,onComplete:afterTweenLiteFun});
			
			maskSp=new Sprite();
			maskSp.graphics.clear();
			maskSp.graphics.beginFill(0x000000,1);
			maskSp.graphics.drawCircle(GV_Map.instance.stage.stageWidth/2,GV_Map.instance.stage.stageHeight/2,260);
			maskSp.graphics.endFill();
			//				GV_Map.instance.addChild(maskSp);
			GV_Map.instance.mask=maskSp;
		}
		
		private var timer:Timer;
		private function afterTweenLiteFun():void
		{
			
			timer=new Timer(1000,1);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE,hanlderFun);
			timer.start();
		}
		
		private function hanlderFun(e:Event):void
		{
			Mouse.show();
			GV_Map.instance.mask=null;
			this.maskSp.graphics.clear();
			this.maskSp=null;
			callBackFun();
			timer.removeEventListener(TimerEvent.TIMER_COMPLETE,hanlderFun);
			timer.stop();
			timer=null;
			PlatformInfo.instance.isMoveScreen=false;
			GU_dealWeapon.instance.playTxwin();
		}
		
		
		public function creatNpcIcon(sourceName:String,picName:String):VI_VectorIcon
		{
			return  VI_VectorIcon.creat({sourceName:sourceName,picName:picName});
		}	
		
		public static function get instance():Deal_SpecialMission
		{
			if(!_instance)_instance=new Deal_SpecialMission();
			return _instance;
		}
		
	}
}
